﻿using EnvDTE80;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using EBFHelper.Nodes;
using EBFHelper.EBFHelperUtil;
using EBFHelper.forms;

using System.Xml;
using System.Diagnostics;

namespace EBFHelper
{
    public partial class EBFHelperForm : Form
    {
        private DTE2 _applicationObject;
        private List<iNode> activeActionXmlList;
        private AutoCompleteStringCollection strings = new AutoCompleteStringCollection();

        public DTE2 applicationObject
        {
            get { return _applicationObject; }
            set { this._applicationObject = value; }
        }

        public EBFHelperForm(DTE2 applicationObject)
        {
            this._applicationObject = applicationObject;
            InitializeComponent();
            
        }

        private int checkconfigourationInfo()
        {
            try
            {
                //获取解决方案的所有项目
                System.Array theProjects = (System.Array)_applicationObject.ActiveSolutionProjects;

                if (theProjects.Length > 0)
                {
                    //遍历解决方案中的每一个项目
                    foreach (EnvDTE.Project theProject in theProjects)
                    {
                        if (!theProject.Name.Contains("WebSite"))
                        {
                            continue;
                        }
                        List<EnvDTE.ProjectItem> configourations = new List<EnvDTE.ProjectItem>();
                        //遍历项目中的每一个元素
                        HelperUtil.checkProjectItemNode(theProject.ProjectItems, configourations);
                        //获取到actions operations opsteps节点
                        if (configourations.Count > 0)
                        {
                            List<iNode> actionNodes = new List<iNode>();
                            List<iNode> operationNodes = new List<iNode>();

                            HelperUtil.findActionsAndOperationsAndOpstepsList(configourations, actionNodes, operationNodes);

                            List<iNode> actionXmlList = HelperUtil.findXmlNodeList(actionNodes);
                            List<iNode> operationXmlList = HelperUtil.findXmlNodeList(operationNodes);

                           activeActionXmlList = new List<iNode>();

                            HelperUtil.addOperationXmlNodeToActionXmlNode(actionXmlList, operationXmlList,activeActionXmlList,strings);
                            
                            HelperUtil.bindSourceToTextBox(ActionListBox, activeActionXmlList);
                         
                            SearchText.AutoCompleteCustomSource = strings;
                            SearchText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                            SearchText.AutoCompleteMode = AutoCompleteMode.Suggest;
                            
                        }
                    }
                }

                return 0;
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message.ToString());
                return 1;
            }
        }

        private void ActionListBox_MouseClick(object sender, MouseEventArgs e)
        {
            int index = ActionListBox.IndexFromPoint(e.X, e.Y);
            ActionListBox.SelectedIndex = index;
            object item = ActionListBox.SelectedItem;
            ActionXmlNode actionXmlNode;
            if (null == (actionXmlNode = item as ActionXmlNode))
            {
                return;
            }
            //TODO
            MyTreeNode tNode = new MyTreeNode();
            tNode.name = actionXmlNode.operationNode.projectItem.Name;
            tNode.Text = actionXmlNode.operationNode.projectItem.Name;
            tNode.path = actionXmlNode.operationNode.projectItem.FileNames[1];

            XmlDocument xmlDoc=new XmlDocument();
            xmlDoc.Load(tNode.path);

            HelperUtil.buildOperationTree(xmlDoc.ChildNodes, tNode.Nodes);

            if (OperationTreeView.Nodes.Count>0)
            {
                OperationTreeView.Nodes.Clear();
                
            }
            OperationTreeView.Nodes.Add(tNode);
            
        }

        private void ActionListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = ActionListBox.IndexFromPoint(e.X, e.Y);
            ActionListBox.SelectedIndex = index;
            object item = ActionListBox.SelectedItem;
            ActionXmlNode actionXmlNode;
            if (null == (actionXmlNode=item as ActionXmlNode))
            {
                return;
            }
            XmlDocument xmlDoc = new XmlDocument();
            string path = actionXmlNode.projectItem.FileNames[1];
            xmlDoc.Load(path);
            ActionXmlNodeDetailInfo aXNDI = new ActionXmlNodeDetailInfo(xmlDoc);
            aXNDI.Show();
        }

        private void OperationTreeView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MyTreeNode tNode;
            if (null == (tNode=OperationTreeView.SelectedNode as MyTreeNode))
            {
                return;
            }
            XmlNodeAtrributeInfo xNAI = new XmlNodeAtrributeInfo(tNode);
            xNAI.Show();
        }

        private void EBFHelperForm_Load(object sender, EventArgs e)
        {
            checkconfigourationInfo();
        }

        private void SearchText_TextChanged(object sender, EventArgs e)
        {   
            if (SearchText.Text.Equals(""))
            {
                HelperUtil.bindSourceToTextBox(ActionListBox, activeActionXmlList);
            }
            else if(SearchText.Text.EndsWith(".xml"))
            {
                foreach(iNode inode in activeActionXmlList)
                {
                    ActionXmlNode actionXmlNode;
                    if (null == (actionXmlNode=inode as ActionXmlNode))
                    {
                        return;
                    }
                    
                    if(SearchText.Text.Equals(actionXmlNode.name))
                    {
                        HelperUtil.bindSourceToTextBox(ActionListBox, actionXmlNode);
                        break;
                    }

                    
                }
            }
            else
            {
                return;
            }
        }

      
        
       
    }
}
